Component({
  properties: {

  },

  data: {
    user: {
      phone: '',
      password: ''
    },
    errors: {
      phone: false,
      code: false
    }
  },

  methods: {
    getVerificationCode() {
        wx.request({
          url: 'https://ufhmfcbjuwjt.sealoshzh.site/user/validCode/phone', 
          method: 'GET',
          header: {
            'Content-Type': 'application/json' 
          },
          data: {
            "phone": this.data.user.phone,
          },
          success(res) {
            console.log('验证码成功');
          },
          fail(err) {
            console.error(err);
          }
      });
    },
    login() {
      const { phone, password } = this.data.user;
      let isValid = true;
      const errors = { phone: false, password: false };
      if (!phone) {
        console.log('手机号不能为空!')
        errors.phone = true;
        isValid = false;
      }
      if (!password) {
        errors.password = true;
        isValid = false;
      }
      this.setData({ errors });
      if (isValid) {
        wx.request({
          url: 'https://ufhmfcbjuwjt.sealoshzh.site/user/login',
          method: 'POST',
          header: {
            'Content-Type': 'application/json' 
          },
          data: {
            "phone": this.data.user.phone,
            "password": this.data.user.password
          },
          success:(res) =>{
            console.log('登陆成功!',res.data);
            if(res.data && res.data.data.tokenName && res.data.data.tokenValue){
              this.saveTokenData(res.data.data.tokenName, res.data.data.tokenValue);
            };
            wx.navigateTo({
              url: '/pages/main/main',
            });
          },
          fail:(err)=> {
            console.error(err);
          }
        })
      }
    },
    onPhoneInput(e) {
      this.setData({
        'user.phone': e.detail.value,
        'errors.phone': false
      })
    },
    onPasswordInput(e) {
      this.setData({
        'user.password': e.detail.value,
        'errors.password': false
      });
    },
    onClose() {
      this.setData({ show: false });
    },
    saveTokenData(tokenName, tokenValue) {
      try {
        wx.setStorageSync('auth_token', {
          tokenName,
          tokenValue,
          expireTime: Date.now() + 24 * 60 * 60 * 1000 
        });
        console.log('Token 保存成功');
      } catch (e) {
        console.error('保存 Token 失败', e);
      }
    },
    getTokenData() {
      try {
        const tokenData = wx.getStorageSync('auth_token');
        if (tokenData && Date.now() < tokenData.expireTime) {
          return tokenData;
        } else {
          return null;
        }
      } catch (e) {
        console.error('获取 Token 失败', e);
        return null;
      }
    },
    getRequestHeader(tokenData) {
      const headers = {
        'Content-Type': 'application/json'
      };
      if (tokenData) {
        headers[tokenData.tokenName] = tokenData.tokenValue;
      }
      return headers;
    }
  } 
})